package com.example.soa_exp2.controller;

import com.example.soa_exp2.common.MessageModel;
import com.example.soa_exp2.common.UserModel;
import com.example.soa_exp2.domain.User;
import com.example.soa_exp2.persistence.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/user")
    public HttpEntity<CollectionModel<UserModel>> getAllUser() {
        List<User> userList = userMapper.selectList(null);
        List<UserModel> userModels = new ArrayList<>();
        for(User user:userList){
            UserModel model = new UserModel(user);
            model.add(linkTo(methodOn(UserController.class).getUserById(user.getUserid())).withSelfRel());
            userModels.add(model);
        }
        CollectionModel<UserModel> models = CollectionModel.of(userModels);
        models.add(linkTo(methodOn(UserController.class).getAllUser()).withSelfRel());
        return new ResponseEntity<>(models, HttpStatus.OK);
    }

    @GetMapping("/user/{userId}")
    public HttpEntity<UserModel> getUserById(@PathVariable String userId){
        User user = userMapper.selectById(userId);
        if(user == null){
            UserModel model = new UserModel(null, "没有查询到用户");
            model.add(linkTo(methodOn(UserController.class).getAllUser()).withRel("allUser"));
            return new ResponseEntity<>(model, HttpStatus.OK);
        }else {
            UserModel model = new UserModel(user);
            model.add(linkTo(methodOn(UserController.class).getUserById(userId)).withSelfRel());
           return new ResponseEntity<>(model, HttpStatus.OK);
        }
    }

    @PostMapping("/user")
    public HttpEntity<UserModel> addUser(@RequestBody User addUser){
        userMapper.insert(addUser);
        UserModel model = new UserModel(addUser);
        model.add(linkTo(methodOn(UserController.class).getUserById(addUser.getUserid())).withSelfRel());
        return new ResponseEntity<>(model, HttpStatus.OK);
    }

    @PutMapping("/user/{userId}")
    public HttpEntity<UserModel> updateUserById(@PathVariable String userId, @RequestBody User newUser){
        newUser.setUserid(userId);
        userMapper.updateById(newUser);
        UserModel model = new UserModel(newUser);
        model.add(linkTo(methodOn(UserController.class).updateUserById(userId, newUser)).withSelfRel());
       return new ResponseEntity<>(model, HttpStatus.OK);
    }

    @DeleteMapping("/user/{userId}")
    public HttpEntity<MessageModel> deleteUserById(@PathVariable String userId){
        userMapper.deleteById(userId);
        MessageModel model = new MessageModel("删除成功！");
        model.add(linkTo(methodOn(UserController.class).getAllUser()).withRel("allUser"));
        return new ResponseEntity<>(model, HttpStatus.OK);
    }
}
